package testGridTree.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import testGridTree.GridTreeDao;
import testGridTree.GridTreeUtil;

/**
 * 普通表格树:简化了处理的步骤,必须有两处要调用GridTreeUtil中的方法.
 * connect me:419723443@qq.com
 */
public class TableTreeAction extends Action {
	// 每页行数
	private static int DEFAULT_PAGE_SIZE = 10;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		GridTreeDao dao = new GridTreeDao();
		List list = new ArrayList();
		// 结果行数
		int totalNum = 0;
		int[] rowStartEnd;
		int len = 0;
		try {
			//你的业务必须要做的事情:查询第一层的节点的总数!!
			totalNum = dao.getFirstLevelCount();
			
			// 必须调用工具类的方法:计算起始行和终止行.
			rowStartEnd = GridTreeUtil.getStartAndEndInfo(request, totalNum,
					DEFAULT_PAGE_SIZE);
			
			//你的业务必须要做的事情:查询显示第一页显示出来的节点以及子节点!
			list = dao.getCommonList(rowStartEnd[0], rowStartEnd[1]);

			// 必须调用工具类的方法:得到json字符串。
			String jsonStr = GridTreeUtil.getJsonStr(list, request);
			
			
			response.setContentType("text/html; charset=UTF-8");
			System.out.println("json串:" + jsonStr);
			PrintWriter out = response.getWriter();
			out.println(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

/***下面是返回的json串的示例,根据设置的analyzeAtServer参数不同,json形式不一样!!默认该值为false,json字符串比较简单!如果两种效率都比较慢的话,就一定要使用
 * lazy=true属性,使用懒加载的表格树!!效率将大大提高! **/
/**
 * (analyzeAtServer=false的情况下)
 * json串:{total:31,page:1,
 * data:[{"disid":"10","disparentId":"","disname":"湖北","isLeaf":""},
 * {"disid":"11","disparentId":"","disname":"北京","isLeaf":""},
 * {"disid":"1101","disparentId":"11","disname":"北京","isLeaf":""},
 * {"disid":"4201","disparentId":"10","disname":"荆州","isLeaf":""},
 * {"disid":"4202","disparentId":"10","disname":"武汉","isLeaf":""},
 * {"disid":"4203","disparentId":"10","disname":"黄石","isLeaf":""},
 * {"disid":"4204","disparentId":"10","disname":"十堰","isLeaf":""},
 * {"disid":"4205","disparentId":"10","disname":"神农架","isLeaf":""},
 * {"disid":"4206","disparentId":"10","disname":"宜昌","isLeaf":""},
 * {"disid":"4207","disparentId":"10","disname":"襄樊","isLeaf":""},
 * {"disid":"4208","disparentId":"10","disname":"鄂州","isLeaf":""},
 * {"disid":"4209","disparentId":"10","disname":"荆门","isLeaf":""},
 * {"disid":"4210","disparentId":"10","disname":"孝感","isLeaf":""},
 * {"disid":"4211","disparentId":"10","disname":"湖北","isLeaf":""}]}
 */

/*下面是输出的格式字符串示例(analyzeAtServer=true的情况下)
json串:{"allCount":31,
"data":[{"disid":"10","disparentId":"","disname":"湖北","isLeaf":""},
{"disid":"11","disparentId":"","disname":"北京","isLeaf":""},
{"disid":"1101","disparentId":"11","disname":"北京","isLeaf":""},
{"disid":"4201","disparentId":"10","disname":"荆州","isLeaf":""},
{"disid":"4202","disparentId":"10","disname":"武汉","isLeaf":""},
{"disid":"4203","disparentId":"10","disname":"黄石","isLeaf":""},
{"disid":"4204","disparentId":"10","disname":"十堰","isLeaf":""}
,{"disid":"4205","disparentId":"10","disname":"神农架","isLeaf":""},
{"disid":"4206","disparentId":"10","disname":"宜昌","isLeaf":""},
{"disid":"4207","disparentId":"10","disname":"襄樊","isLeaf":""},
{"disid":"4208","disparentId":"10","disname":"鄂州","isLeaf":""},
{"disid":"4209","disparentId":"10","disname":"荆门","isLeaf":""},
{"disid":"4210","disparentId":"10","disname":"孝感","isLeaf":""},
{"disid":"4211","disparentId":"10","disname":"湖北","isLeaf":""}],
"parents":["_node11","_node10"],"pageSize":2,
"firstLevelNodes":["_node10","_node11"],
"parentToChildMap":{"_node11":["_node1101"],
"_node10":["_node4201","_node4202","_node4203","_node4204","_node4205","_node4206","_node4207",
"_node4208","_node4209","_node4210","_node4211"],"_node":["_node10","_node11"]},
"idToParent":{"_node11":"_node","_node4206":"_node10","_node4202":"_node10",
"_node4203":"_node10","_node4211":"_node10","_node4207":"_node10","_node4210":"_node10",
"_node10":"_node","_node4204":"_node10","_node4208":"_node10","_node1101":"_node11",
"_node4205":"_node10","_node4209":"_node10","_node4201":"_node10"},"currentPage":1,
"idToNodeMap":{"_node11":{"disid":"11","disparentId":"","disname":"北京","isLeaf":""},
"_node4206":{"disid":"4206","disparentId":"10","disname":"宜昌","isLeaf":""},
"_node4202":{"disid":"4202","disparentId":"10","disname":"武汉","isLeaf":""},
"_node4203":{"disid":"4203","disparentId":"10","disname":"黄石","isLeaf":""},
"_node4211":{"disid":"4211","disparentId":"10","disname":"湖北","isLeaf":""},
"_node4207":{"disid":"4207","disparentId":"10","disname":"襄樊","isLeaf":""},
"_node4210":{"disid":"4210","disparentId":"10","disname":"孝感","isLeaf":""},
"_node10":{"disid":"10","disparentId":"","disname":"湖北","isLeaf":""},
"_node4204":{"disid":"4204","disparentId":"10","disname":"十堰","isLeaf":""},
"_node4208":{"disid":"4208","disparentId":"10","disname":"鄂州","isLeaf":""},
"_node1101":{"disid":"1101","disparentId":"11","disname":"北京","isLeaf":""},
"_node4205":{"disid":"4205","disparentId":"10","disname":"神农架","isLeaf":""},
"_node4209":{"disid":"4209","disparentId":"10","disname":"荆门","isLeaf":""},
"_node4201":{"disid":"4201","disparentId":"10","disname":"荆州","isLeaf":""}}}
 * */
